package com.shushan.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;

import com.android.net.bean.NetRetBean;
import com.android.net.core.common.RequestType;
import com.android.net.helper.NetHelper;
import com.android.net.listener.common.CallbackCode;
import com.android.net.url.UrlParse;
import com.shushan.network.UrlCommParse;
import com.shushan.network.UrlConst;
import com.shushan.network.listener.NetStrCommListener;
import com.shushan.shushanbao.BabyApplication;

/**
 * @ClassName: PostExerciseServer
 * @Desciption: //TODO
 * @author: yichaohua
 * @date: 2018-12-14
 */
public class PostExerciseServer extends Service {

    private final static String TAG = "PostExerciseServer";


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if(intent != null){
            int exercisesType = intent.getIntExtra("exercisesType",0);
            int paperId = intent.getIntExtra("paperId",0);
            String answerJson = intent.getStringExtra("answerJson");

            Log.i(TAG,"onStartCommand() paperId=" + paperId + "\nanswerJson=" + answerJson);

            postExercise(exercisesType,paperId,answerJson);
        }

        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private void postExercise(int exercisesType,int paperId,String answerJson) {
        if(paperId <= 0 || TextUtils.isEmpty(answerJson) || answerJson.equals("[]")){
            return;
        }

        UrlParse urlParse = new UrlCommParse(this, UrlConst.BASE_URL).appendRegion(UrlConst.POST_ANSWER_RECORD);
        urlParse.putValue("child_id", BabyApplication.getInstance().getCurrentChildId())
                .putValue("paper_id",paperId)
                .putValue("exercises_type",exercisesType)
                .putValue("answer",answerJson);

        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(urlParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetStrCommListener() {


                    @Override
                    protected void onSuccess(String data) {
                    }

                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
                    }
                });
    }
}
